package com.shengqin.utils;

import com.shengqin.entity.PlanApplyProject;

import java.util.Comparator;

public class StringAndNumberSorter {
    
    public static class AlphaNumericComparator implements Comparator<String> {
        @Override
        public int compare(String s1, String s2) {
            String[] parts1 = s1.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            String[] parts2 = s2.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            
            int len = Math.min(parts1.length, parts2.length);
            for (int i = 0; i < len; i++) {
                String part1 = parts1[i];
                String part2 = parts2[i];
                
                // 如果两部分都是数字
                if (Character.isDigit(part1.charAt(0)) && 
                    Character.isDigit(part2.charAt(0))) {
                    int num1 = Integer.parseInt(part1);
                    int num2 = Integer.parseInt(part2);
                    if (num1 != num2) {
                        return num1 - num2;
                    }
                } else {
                    int result = part1.compareTo(part2);
                    if (result != 0) {
                        return result;
                    }
                }
            }
            return parts1.length - parts2.length;
        }
    }

    public static class SchoolGroupNoComparator implements Comparator<PlanApplyProject> {
        @Override
        public int compare(PlanApplyProject s1, PlanApplyProject s2) {
            String[] parts1 = s1.getGroupSchoolNo().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            String[] parts2 = s2.getGroupSchoolNo().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

            int len = Math.min(parts1.length, parts2.length);
            for (int i = 0; i < len; i++) {
                String part1 = parts1[i];
                String part2 = parts2[i];

                // 如果两部分都是数字
                if (Character.isDigit(part1.charAt(0)) &&
                        Character.isDigit(part2.charAt(0))) {
                    int num1 = Integer.parseInt(part1);
                    int num2 = Integer.parseInt(part2);
                    if (num1 != num2) {
                        return num1 - num2;
                    }
                } else {
                    int result = part1.compareTo(part2);
                    if (result != 0) {
                        return result;
                    }
                }
            }
            return parts1.length - parts2.length;
        }
    }
}